<?php
/*
Plugin Name: 唐诗宋词(chinese poem)
Plugin URI: http://mifunny.info/wp-plugin-chinese-poem-84.html
Description: 随机显示唐诗宋词
Version: 0.1
Author: LD King
Author URI: http://mifunny.info
*/

/**
 * 随机提取一首诗
 *
 */
class LD_Poem {
	private $poem_src, $poem_file;
	private $poem; //Poem_Inter Object
	//Chinese Poem's folder PATH and URL
	private $plugin_dir, $plugin_url; 

	/**
	 * 选泽一个模块,构造函数
	 *
	 * @param array $files 默认只有三个模块：唐诗300首、宋诗100首、宋词
	 * @param bool $ccs 是否加载CSS风格
	 */
	public function __construct($files = 
		array('tang300', 'song100', 'songproses'), $css = TRUE) 
	{	
		//get the plugin path 
		$wp_plugin_dir = ( defined('WP_PLUGIN_DIR') ) ? WP_PLUGIN_DIR : ABSPATH . PLUGINDIR;
		$this->plugin_dir = $wp_plugin_dir."/".dirname(plugin_basename (__FILE__))."/";
		//get the plugin url 
		$wp_plugin_url = ( defined('WP_PLUGIN_URL') ) ? WP_PLUGIN_URL : get_option('siteurl') . PLUGINDIR;
		$this->plugin_url = $wp_plugin_url."/".dirname(plugin_basename (__FILE__))."/";
		
		if (count($files) == 0) {
			echo "<p>I don't understand ".$files."</p>";
			exit;
		}
		//add_css ？
		if ($css == TRUE) {
			$this->add_css();
			//add_action('wp_head', array(&$this, 'add_css'));
		}
		for ($i=0; $i<count($files); $i++) {
			if (!file_exists($this->plugin_dir.$files[$i])) {
				echo '<p>File '.$this->plugin_url.$files[$i].' is not exists.</p>';
				exit;
			}				
		} //END for file_exists
		//poem source based random number
		$rand_num = mt_rand(0, count($files)-1);
		$this->poem_file = $files[$rand_num];
		$this->poem_src = $this->get_poem($this->plugin_dir.$this->poem_file);
	} //END func __construct

	/**
	 * 从文件中随机抽取一首诗
	 *
	 * @param string $filename
	 * @return string
	 */
	private function get_poem($filename) {
		@ $fb = fopen($filename,'rb',TRUE); //open file
		if (!$fb) 	{
			echo "<p>I can't open this file: ".$filename."<p>";
			exit;
		}
		$n = 0; // the number of poem
		while (!feof($fb)) { //count poems
			$tang = fgets($fb, 999);
			if ( ereg('[%]', $tang) )
			$n++;
		}
		$rand_num = mt_rand(1, $n); //random number
		rewind($fb); //back to 0 node
		$begin = $end = 0; 
		for($num = 0; !feof($fb); ) {
			$tang = fgets($fb,999);
			if (ereg('[%]',$tang)) {
				$num++;
				if ($num == $rand_num ) {
					$end = ftell($fb);
					break; //get poem node
				}
				else
					$begin = ftell($fb);			 
			}
		} //END for feof($fb)
		$len=$end - $begin;
		fseek($fb, $begin, SEEK_SET);
		$result = fread($fb,$len);
		fclose($fb);  //close file

		return $result;
	} //END func get_poem_src	
	
	public function __get($name){
		return $this->$name;
	}
	
	/**
	 * 
	 */
	public function add_css() {
		echo "<link rel=\"stylesheet\" href=\"".$this->plugin_url."poem.css\" />\n";
	}

	/**
	 * 调用格式化接口，输出诗词
	 *
	 */
	public function display(Poem_Inter $poem) {
		$this->poem=$poem;
		echo $this->poem->format_poem($this->poem_src);
	} //END func display
} //END class LD_Poem	

/**
 * 诗词接口，便于类LD_Poem的调用
 *
 */
interface Poem_Inter {
	/**
	 * 把从文件中读取的字符串转换成HTML代码
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src);
} //END inter Poem_Inter

/**
 * tang300:     唐诗300首
 */
class tang300 implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		
		$result = "<span class='poem-title' >".$poem_arr[0]."</span><br />";
		$result .= "<span class='poem-author' >".$poem_arr[1]."</span><br />";
		$result .= "<span class='poem-content' >".$poem_arr[2];
		for ($n=3; $n <= count($poem_arr)-3; $n++ ) {
			$result .= "<br />".$poem_arr[$n];
		}
		$result .= "</span>";
		return ereg_replace(chr(10),'',$result);
	}	
} //END class tang300

/**
 * song100:     宋诗100首
 */
class song100 implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		
		$result = "<span class='poem-title' >".$poem_arr[0]."</span><br />";
		$result .= "<span class='poem-author' >".$poem_arr[1]."</span><br />";
		$result .= "<span class='poem-content' >".$poem_arr[2];
		for ($n=3; $n <= count($poem_arr)-3; $n++ ) {
			$result .= "<br />".$poem_arr[$n];
		}
		$result = $result."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class song100

/**
 * songproses:  宋词
 */
class songproses implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >";
		for ($n=0; $n <= count($poem_arr)-4; $n++)
			$result .= $poem_arr[$n]."<br />";
		$poem_at = split('@',$poem_arr[count($poem_arr)-3]);
		$result .= "</span><span class='poem-author' >".$poem_at[0]
			."</span>   <span class='poem-title' >".$poem_at[1]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class format_poem

/**
 * caigentan:   菜根谭
 */
class caigentan implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >".$poem_arr[0]
			."</sapn><br /><span class='poem-title' >".$poem_arr[1]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class caigentan

/**
 * dao:	     道德经
 */
class dao implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >";
		for ($n=0; $n <= count($poem_arr)-4; $n++)
			$result .= $poem_arr[$n]."<br />";
		$result .= "</span><span class='poem-title' >".$poem_arr[count($poem_arr)-3]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class dao

/**
 * joke:	     笑话集
 */
class joke implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >";
		for ($n=0; $n <= count($poem_arr)-4; $n++)
			$result .= $poem_arr[$n]."<br />";
		$result .= $poem_arr[count($poem_arr)-3]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class joke

/**
 * maoshici:    毛泽东诗词
 */
class maoshici implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		
		$result = "<span class='poem-title' >".$poem_arr[0]."</span><br />";
		$result = $result."<span class='poem-author' >".$poem_arr[1]."</span><br />";
		$result = $result."<span class='poem-content' >".$poem_arr[2];
		for ($n=3; $n <= count($poem_arr)-3; $n++ ) {
			$result .= "<br />".$poem_arr[$n];
		}
		$result = $result."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class maoshici

/**
 * maoyulu:     毛泽东语录
 */
class maoyulu implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >";
		for ($n=0; $n <= count($poem_arr)-4; $n++)
			$result .= $poem_arr[$n]."<br />";
		$result .= "</span><span class='poem-title' >".$poem_arr[count($poem_arr)-3]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class maoyulu

/**
 * proverb:     谚语
 */
class proverb implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >".$poem_arr[0]
			."</sapn><br /><span class='poem-title' >".$poem_arr[1]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class proverb

/**
 * zengguang:   增广贤文
 */
class zengguang implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split(chr(10),$poem_src);
		$result = "<span class='poem-content' >".$poem_arr[0]
			."</sapn><br /><span class='poem-title' >".$poem_arr[1]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class zengguang

/**
 * Add-Ons
 */



/**
 * [外部调用函数]随机显示一首诗歌
 *
 * @param array $model 默认只有三个模块：唐诗300首、宋诗100首、宋词
 * @param bool $ccs 是否加载CSS风格
 */
function show_chinese_poem(
	$modules = array('tang300', 'song100', 'songproses'), $css = TRUE ) 
{
	$cn_poem = new LD_Poem($modules, $css);
	$cn_poem->display(new $cn_poem->poem_file);
	unset($cn_poem);
} //END func show_chinese_poem

?>